import {
  Message
} from "element-ui"

function addQueryString(url, params) {
  var str = '';
  for (var Key in params) {
    str += Key + '=' + params[Key] + '&';
  }
  return `${url}?${str.substr(0, str.length -1)}`
}

function request($axios, callback) {
  function get(url, data) {
    return $axios.get(addQueryString(url, data));
  };

  function post(url, data) {
    return $axios.post(url, data);
  }
  callback && callback({
    /**文件下载 */
    downloadAPI(data = {
      fileId: 1
    }) {
      return get("/api/webfile/download/resource", data)
    },
    loginAPI(data = {}, callback) {
      return post("/api/web/auth/login", data).then(res => {
        let {
          token,
          user
        } = res.data;
        window.localStorage.setItem('token_zb', token);
        window.localStorage.setItem("userInfo", JSON.stringify(user));
        Message.success("登录成功");
        setTimeout(() => {
          window.location.reload();
        }, 500);
        callback && callback(res)
      });
    },
    /**上传文件 */
    uploadFile(form = {}) {
      let formData = new FormData();
      for (let [key, val] of Object.entries(form)) {
        formData.append(key, val)
      }
      return post("/api/webfile/upload", formData)
    },
    getHomeAPI(data = {}) {
      return post("/api/web/home/meun", data)
    },
    webHomeNewsAPI(data) {
      return post("/api/web/home/news", data)
    },
    webHomeBiddingAPI(data) {
      return post("/api/web/home/bidding", data)
    },
    registerAPI(data = {}) {
      return post("/api/web/auth/register", data)
    },
    captchaImageAPI(data = {}) {
      return get("api/web/auth/captchaImage", data)
    },
    /**支付宝支付 */
    webUserCzAPI(data) {
      return post("/api/web/user/cz", data)
    },

    userInfoAPI(data) {
      return post("/api/web/user/info", data)
    },

    /** 用户充值记录 */
    webUserCzjlAPI(data) {
      return post("/api/web/user/czjl", data)
    },
    webUserXfjlAPI(data) {
      return post("/api/web/user/xfjl", data)
    },
    userWdfbAPI(data) {
      return post("/api/web/user/wdfb", data)
    },
    userPriceAPI(data){
      return post("/api/web/user/price", data)
    },
    /**支付二维码 */
    userReleaseCodeAPI(data){
      return post("/api/web/user/release/code", data)
    },
    /**余额支付 */
    releasePayAPI(data){
      return post("/api/web/user/release", data)
    },
    noticeListAPI(data){
      return post("/api/web/notice/list", data)
    },
    noticeInfoAPI(data){
      return post("/api/web/notice/info", data)
    },
    docListAPI(data) {
      return post("/api/web/doc/list", data)
    },
    docInfoAPI(data){
      return post("/api/web/doc/info", data)
    },
    homeImgAPI(data){
      return post("/api/web/home/img", data)
    }
  })
};
export default request;
